<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="query"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderByPhone"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderlist" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="下单手机号"
          prop="phone"
          align="center"
        ></el-table-column>
        <el-table-column label="电影图片" prop="imgUrl" align="center">
          <template slot-scope="scope">
            <img class="img" :src="scope.row.imgUrl" alt="" srcset="" />
          </template>
        </el-table-column>
        <el-table-column
          label="电影名字"
          prop="movieTitle"
          align="center"
        ></el-table-column>
        <el-table-column label="电影简介" prop="content" align="center">
          <template slot-scope="scope">
            <p class="showOverTooltip">{{ scope.row.content }}</p>
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          prop="category"
          align="center"
          width="150px"
        >
          <template slot-scope="scope">
            <el-tag>{{ scope.row.category }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="电影价格(元)"
          prop="price"
          align="center"
          width="150px"
        ></el-table-column>
        <el-table-column label="操作" width="180px" align="center">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeMovieById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      orderlist: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orderAll");
      if (res.status !== 200) return this.$message.error("获取失败");
      this.$message.success("获取成功");
      this.orderlist = res.data;
    },
    async getOrderByPhone() {
      const { data: res } = await this.$http.get("orderByPhone", {
        params: { query: this.query },
      });
      if (res.status !== 200) return this.$message.error("获取失败");
      this.$message.success("获取成功");
      this.orderlist = res.data;
    },
      //根据id删除对应的用户信息
    async removeMovieById(id) {
      //弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该订单, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      //如果用户确认删除，则返回值为字符串 confirm
      //如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete('order/' + id)
      if (res.status !== 200) {
        return this.$message.error('删除订单失败！')
      }
      this.$message.success('删除订单成功！')
      this.getOrderList()
    }
  },
};
</script>

<style lang="less" scoped>
.img {
  width: 100px;
  height: 150px;
  margin: 0 auto;
}
.showOverTooltip {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>